package com.mxfcode.back.controller;

import com.mxfcode.annotation.LoginRequired;
import com.mxfcode.back.service.RoleService;
import com.mxfcode.bbs_common.bean.BBSRank;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.entity.TSRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author moxiufeng
 * @date 2021/3/7 19:05
 */
@Controller
@RequestMapping("/back/role")
@LoginRequired
public class RoleController {
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/getRoleList", method = RequestMethod.GET)
    @ResponseBody
    public PageDataResult getRoleList(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize) {
        logger.info("获取角色列表");
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取角色列表
            pdr = roleService.getRoleList(pageNum ,pageSize);
            logger.info("角色列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("角色列表查询异常！", e);
        }
        return pdr;
    }

    @PostMapping("setRole")
    @ResponseBody
    public Map<String,Object> setRole(TSRole role) {
        logger.info("设置角色[新增或更新]！role:" + role);
        Map<String,Object> data = new HashMap();
        if(role.getId() == null){
            //新增角色
            data = roleService.addRole(role);
        }else{
            //修改角色
            data = roleService.updateRole(role);
        }
        return data;
    }

    @PostMapping("updateRoleStatus")
    @ResponseBody
    public Map<String,Object> updateRoleStatus(@RequestParam("id") int id,@RequestParam("status") Integer status) {
        logger.info("删除/恢复角色！id:" + id+" status:"+status);
        Map<String, Object> data = new HashMap<>();
        if(status == 0){
            //删除角色
            data = roleService.delRole(id,status);
        }else{
            //恢复角色
            data = roleService.recoverRole(id,status);
        }
        return data;
    }

    @GetMapping("getRoles")
    @ResponseBody
    public List<TSRole> getRoles(){
        logger.info("获取角色列表");
        return roleService.getRoles();
    }
}
